## Dan Mallinson (mallinsond@gmail.com)
## Code for calculating neighbor adoptions
## 04-01-2015

#########################################################################
##						Code for a single policy						   ##
#########################################################################

#install.packages("foreign") #Uncomment to install foreign package
library(foreign)

raw.data <- read.csv("example_adopts_single.csv") #read in event data (including your covariates)

data <- raw.data

neighbor <- read.csv("neighbor_raw.csv") #read in contiguous dyad dataset

unique.policies <- as.matrix(unique(data$policy)) #numeric list of included policies

df <- as.data.frame(matrix(NA, nrow=1, ncol=ncol(data)+2)) #Create blank dataset for appending data

names(df) <- c(colnames(data), "neighbor_sum", "neighbor_prop") #Add column names for count and proportion of neighbor adoptions variables

vars <- c("policy", "state", "year","state_no","adopt")
adopts <- data[which(data$adopt==1),]
adopts <- adopts[vars]
  for(j in 1:nrow(data)){
    use.obs <- data[j,]
    if(use.obs$year==min(data$year, na.rm=TRUE) | is.na(use.obs$year)){
      use.obs$neighbor_sum <- 0
      use.obs$neighbor_prop <- 0
      df <- rbind(df, use.obs)}
    else{
      obs.neighbor <- neighbor[which(neighbor$state_no == use.obs$state_no),]
      year <- as.numeric(use.obs$year)
      use.adopts <- adopts[which(adopts$year < year),]
      obs.neighbor$count <- 0
      for(k in 1:nrow(use.adopts)){
        obs.neighbor$count[use.adopts[k,4]==obs.neighbor$neighbor_no] <- 1
      }
      use.obs$neighbor_sum <- sum(obs.neighbor$count)
      use.obs$neighbor_prop <- mean(obs.neighbor$count)
      df <- rbind(df, use.obs)}
  }
df <- df[-1,] #Delete row of null values

#Save new dataset
write.csv(df, "adopts_with_neighbor.csv")
write.dta(df, "adopts_with_data.dta")


#########################################################################
##						Code for a multiple policies			     	   ##
#########################################################################


##  Read in an adoption dataset that includes relevant covariates and  ##
##   and the .csv file which includes each dyad of contiguous states   ##


#install.packages("foreign") #Uncomment to install foreign package
library(foreign)

raw.data <- read.csv("example_adopts_multiple.csv") #read in event data (including your covariates)

data <- raw.data

neighbor <- read.csv("neighbor_raw.csv") #read in contiguous dyad dataset

unique.policies <- as.matrix(unique(data$policy)) #numeric list of included policies

df <- as.data.frame(matrix(NA, nrow=1, ncol=ncol(data)+2)) #Create blank dataset for appending data

names(df) <- c(colnames(data), "neighbor_sum", "neighbor_prop") #Add column names for count and proportion of neighbor adoptions variables

for(i in 1:nrow(unique.policies)){
  use.policy <- unique.policies[i,]
  use.data <- data[which(data$policy==use.policy),]
  vars <- c("policy", "state", "year","state_no","adopt")
  adopts <- use.data[which(use.data$adopt==1),]
  adopts <- adopts[vars]
  for(j in 1:nrow(use.data)){
    use.obs <- use.data[j,]
    if(use.obs$year==min(use.data$year, na.rm=TRUE) | is.na(use.obs$year)){
      use.obs$neighbor_sum <- 0
      use.obs$neighbor_prop <- 0
      df <- rbind(df, use.obs)}
    else{
      obs.neighbor <- neighbor[which(neighbor$state_no == use.obs$state_no),]
      year <- as.numeric(use.obs$year)
      use.adopts <- adopts[which(adopts$year < year),]
      obs.neighbor$count <- 0
      for(k in 1:nrow(use.adopts)){
        obs.neighbor$count[use.adopts[k,4]==obs.neighbor$neighbor_no] <- 1
      }
      use.obs$neighbor_sum <- sum(obs.neighbor$count)
      use.obs$neighbor_prop <- mean(obs.neighbor$count)
      df <- rbind(df, use.obs)}
  }
}
df <- df[-1,] #Delete row of null values

#Save new dataset
write.csv(df, "adopts_with_neighbor.csv")
write.dta(df, "adopts_with_data.dta")
